<div class="page-header">
    <h1>
        SN <?php echo ($this->type == 2) ? "Member" : "Agen"; ?>
        <small>
            <i class="icon-double-angle-right"></i>
            SN <?php echo ($this->type == 2) ? "Member" : "Agen"; ?>

        </small>
    </h1>
</div>

<?php

if ($this->msg) {
    ?>
    <div class="alert alert-info">
        <button type="button" class="close" data-dismiss="alert">
            <i class="icon-remove"></i>
        </button>

        <strong>
            <i class="icon-ok"></i>
        </strong>

        <?php echo $this->msg; ?>
        <br />
    </div>
<?php
}


if ($this->errmsg) {
    ?>
    <div class="alert alert-danger">
        <button type="button" class="close" data-dismiss="alert">
            <i class="icon-remove"></i>
        </button>

        <strong>
            <i class="icon-remove"></i>
            
        </strong>

        <?php echo $this->errmsg; ?>
        <br />
    </div>
<?php
}
?>

<div class="row">
        <div class="col-xs-11">
            <!-- PAGE CONTENT BEGINS -->
            <form id="frm" class="form-horizontal" role="form" action="" method="post">
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="sn"> SN </label>

                    <div class="col-sm-9">
                        <input class="col-xs-10 col-sm-5" type="text" style="width:150px" name="sn" id="sn" value="<?php echo (isset($this->data['sn'])) ? $this->data['sn'] : ""; ?>"></input>
                        <input type="hidden" name="snList" id="snList">
                        <input type="hidden" name="flag" id="flag">
                    </div>
                </div>
            
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-right" for="balance2">Status</label>
                    <div class="col-sm-9">
                        <select class="width-30 chosen-select" name="stts" id="stts" class="col-xs-10 col-sm-5">
                            <option value="">Semua Status</option>
                            <option value="1">Pasif/Tidak Aktif</option>
                            <option value="2">Aktif</option>
                            <option value="3">Sudah Digunakan</option>
                        </select>
                        &nbsp;
                        <button class="btn btn-info" type="button" onclick="getData()">
                            <i class="icon-ok bigger-110"></i>
                            Cari
                        </button>
                    </div>
                </div>

   
        
        </form>
        <br/>

        <table id="grid-table"></table>

        <div id="grid-pager"></div>

        <script type="text/javascript">
                var $path_base = "<?php echo $this->url()?>";//this will be used in gritter alerts containing images
        </script>


        <!--div class="form-group">
            <div class="col-md-offset-3 col-md-9">
            <input type="button" class="btn btn-info" value="Aktifkan Yang Dipilih" onclick="if (confirm('Aktifkan yang dipilih ?')) return submitSN('1')" />
            <input type="button" class="btn btn-info" value="Aktifkan Semua" onclick="if (confirm('Aktifkan semua ?')) return submitSN('2')" />
            <input type="button" class="btn btn-info" value="Pasifkan Semua" onclick="if (confirm('Pasifkan semua ?')) return submitSN('3')" />
            </div>
        </div-->

        <!-- PAGE CONTENT ENDS -->
    </div>
</div>
<script>
        jQuery(function($) {

                jQuery('#grid-table').jqGrid({
                    url:'<?php echo $this->url() ?>/../../jsondata/getagentsn?search=true&searchField=<?php echo ($this->type == 2) ? "mitra_id" : "agent_id"; ?>&searchOper=eq&searchString=<?php echo $this->memberId?>&type=<?php echo $this->type?>',
                    datatype: "json",
                    height: 150,
                    colNames:['SN','PIN','Status', 'Tgl Pembelian'],
                    colModel:[
                        {name:'SN',index:'sn_code', width:300},
                        {name:'PIN',index:'sn_pin', width:300},
                        {name:'Status',index:'sn_status', width:300},
                        {name:'Tgl Pembelian',index:'buy_dat', width:300},
                    ],
                    rowNum:10,
                    rowList:[10,20,30],
                    pager: '#grid-pager',
                    sortname: 'sn',
                    viewrecords: true,
                    sortorder: "desc",
                    //multiselect: true,
                    multiboxonly: true,
                    caption:"Daftar SN",
                    rownumbers: true,
                    loadComplete : function() {
                            var table = this;
                            setTimeout(function(){
                                    updateActionIcons(table);
                                    updatePagerIcons(table);
                                    enableTooltips(table);
                            }, 0);
                    },

                    autowidth: true
                });

                jQuery('#grid-table').jqGrid('navGrid','#grid-pager',{edit:false,add:false,del:false});


                //navButtons
                jQuery('#grid-table').jqGrid('navGrid','#grid-pager',
                        { 	//navbar options
                                search: true,
                                searchicon : 'ui-icon-search orange',
                                refresh: true,
                                refreshicon : 'ui-icon-refresh green'
                        }
                )




                //unlike navButtons icons, action icons in rows seem to be hard-coded
                //you can change them like this in here if you want
                function updateActionIcons(table) {

                        var replacement =
                        {
                                'ui-icon-search' : 'ui-icon-search orange',
                                'ui-icon-refresh' : 'ui-icon-refresh green'
                        };
                        $(table).find('.ui-pg-div span.ui-icon').each(function(){
                                var icon = $(this);
                                var $class = $.trim(icon.attr('class').replace('ui-icon', ''));
                                if($class in replacement) icon.attr('class', 'ui-icon '+replacement[$class]);
                        })

                }

                //replace icons with FontAwesome icons like above
                function updatePagerIcons(table) {
                        var replacement =
                        {
                                'ui-icon-seek-first' : 'icon-double-angle-left bigger-140',
                                'ui-icon-seek-prev' : 'icon-angle-left bigger-140',
                                'ui-icon-seek-next' : 'icon-angle-right bigger-140',
                                'ui-icon-seek-end' : 'icon-double-angle-right bigger-140'
                        };
                        $('.ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon').each(function(){
                                var icon = $(this);
                                var $class = $.trim(icon.attr('class').replace('ui-icon', ''));

                                if($class in replacement) icon.attr('class', 'ui-icon '+replacement[$class]);
                        })
                }

                function enableTooltips(table) {
                        $('.navtable .ui-pg-button').tooltip({container:'body'});
                        $(table).find('.ui-pg-div').tooltip({container:'body'});
                }

                //var selr = jQuery(grid_selector).jqGrid('getGridParam','selrow');

        });

        function cek(flag){
            var msg = [];

            //var xx = $('#RO').datagrid('getData');
            //alert(xx.length);

            var rows;
            rows = jQuery("#grid-table").jqGrid('getGridParam','selarrrow');

            if (rows == "" && flag == 1) msg.push('* SN harus dipilih');

            if (msg.length == 0) return true;
            else{                
                alert(msg.join("\n"));
                return false;
            }
        }

        function submitSN(flag){
                if (cek(flag)){
                    var ss = [];
                    //var rows = $('#RO').datagrid('getSelections');
                    var rows = jQuery("#grid-table").jqGrid('getGridParam','selarrrow');

                    for(var i=0; i<rows.length; i++){
                            var row = rows[i];
                            var ret = jQuery("#grid-table").jqGrid('getRowData',row);
                            //ss.push('<span>'+row.trans_dtm+":"+row.productid+":"+row.attr1+'</span>');
                            ss.push("'"+ret.SN+"'");
                    }

                    var dataSN = ss.join(',');
                    //alert(dataSN);
                    $('#snList').val(dataSN);
                    $('#flag').val(flag);
                    //$.messager.alert('Info', ss.join(','));

                    $('#frm').submit();
               }
        }


        function getData(){

            var sn = $('#sn').val();
            var stts = $('#stts').val();

            jQuery("#grid-table").jqGrid('setGridParam',{
                url:"<?php echo $this->url() ?>/../../jsondata/getagentsn?type=<?php echo $this->type;?>&sn="+sn+"&stts="+stts+"&search=true&searchField=<?php echo ($this->type == 2) ? "mitra_id" : "agent_id"; ?>&searchOper=eq&searchString=<?php echo $this->memberId?>"
            }).trigger("reloadGrid");
            

       }
</script>




